package leetcode;

public class RotateImage {

	public static void main(String[] args) {
		RotateImage rotateImage = new RotateImage();
		int[][] matrix = {{1,2,},{3,4}};
		rotateImage.rotate(matrix);
		
	}
	public void rotate(int[][] matrix) {
		if (matrix == null || matrix.length <= 0) {
			return;
		}
		int n = matrix.length;
		// https://discuss.leetcode.com/topic/9744/ac-java-in-place-solution-with-explanation-easy-to-understand
		// great solution!!!
		// the idea is first we transport, and then we swap column
		// 1 2 3
		// 4 5 6
		// 7 8 9
		// after transport
		// 1 4 7
		// 2 5 8
		// 3 6 9
		// and after swap colomn
		// 7 4 1
		// 8 5 2
		// 9 6 3
		int temp;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < i; j++) {
				temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
		}
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n / 2; j++) {
				System.out.println("i: " + i + " j: " + j);
				temp = matrix[i][j];
				matrix[i][j] = matrix[i][n - 1 - j];
				matrix[i][n - 1 - j] = temp;
			}
		}
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}
	}
}
